﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace TodayHumor
{
    public partial class Login : PhoneApplicationPage
    {
        SettingsModule settings = new SettingsModule(); LoginModule LoginModule = new LoginModule();

        public Login()
        {
            InitializeComponent();
        }

        #region UI 이벤트 핸들러
        private void LoginPwTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            PasswordWatermark.Opacity = 0; LoginPwTextBox.Opacity = 100;
        }

        private void LoginPwTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            var passwordEmpty = string.IsNullOrEmpty(LoginPwTextBox.Password);
            PasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
            LoginPwTextBox.Opacity = passwordEmpty ? 0 : 100;
        }

        private void LoginTextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (!string.IsNullOrEmpty(tb.Text))
            {
                Brush foregroundBrush = (Brush)Application.Current.Resources["PhoneTextBoxForegroundBrush"];
                if (tb.Foreground != foregroundBrush)
                    tb.Foreground = foregroundBrush;
            }
        }

        private void BackBtnClick(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
        #endregion

        #region 로그인 이벤트 핸들러
        private async void LoginButtonClick(object sender, EventArgs e)
        {
            Utilities.GeneratePI(this, "로그인하는 중입니다..");
            if (await LoginModule.Login(LoginIdTextBox.Text, LoginPwTextBox.Password, AutoLoginCheckBox.IsChecked ?? false))
            {
                // 성공하면 메인 페이지로 다시 돌아간다.
                NavigationService.GoBack();
            }
            else
            {
                SystemTray.SetProgressIndicator(this, null);
            }
        }
        #endregion
    }
    public class BoolToOpacityBackConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                if (!((bool)value))
                {
                    return 1;
                }
                return 0;
            }
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class BoolToOpacityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                if (!((bool)value))
                {
                    return 0;
                }
                return 1;
            }
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}